package com.ssk.sh.web.controller;

import com.ssk.sh.core.anno.NotControllerResponseAdvice;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Objects;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.template.st.StTemplateRenderer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author T0220165
 */
@RestController
@Tag(name = "Ai")
public class AiController {

    //    private final ChatClient chatClient;
//
//    public AiController(ChatClient.Builder chatClientBuilder) {
//        this.chatClient = chatClientBuilder.build();
//    }
    private final ChatModel chatModel;

    public AiController(ChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping(value = "/ai" /*,produces = "text/event-stream"*/)
    @NotControllerResponseAdvice
    Flux<String> generation(String userInput) {
        ChatClient.Builder builder = ChatClient.builder(chatModel);
        ChatClient customChatClient = builder
                .defaultSystem("You are a helpful assistant.")
                .build();
//        return customChatClient.prompt()
//                .user(userInput)
//                .call()
//                .content();

        return Objects.requireNonNull(customChatClient.prompt()
//                .user(u -> u
//                        .text("Tell me the names of 5 movies whose soundtrack was composed by <composer>")
//                        .param("composer", "John Williams"))
                .user(userInput)
                .templateRenderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build())
                .stream()

//                .chatClientResponse();
                .content());

//        return Flux.fromStream().cont;
    }
}
